package com.lzk.auth.service.impl;

import com.lzk.auth.mapper.OrderMapper;
import com.lzk.auth.service.SysUserService;
import com.lzk.model.system.SysOrder;
import com.lzk.model.system.SysProduct;
import com.lzk.auth.mapper.ProductMapper;
import com.lzk.auth.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzk.model.system.SysUser;
import com.lzk.vo.system.ShoppingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzk
 * @since 2023-12-29
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, SysProduct> implements ProductService {


    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public void order(ShoppingVo shoppingVo) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String currentPrincipalName = authentication.getName();
        SysUser user = sysUserService.getUserByUsername(currentPrincipalName);
        SysOrder sysOrder=new SysOrder();
        LocalDateTime dateTime = LocalDateTime.now();
        sysOrder.setOrderTime(dateTime);
        sysOrder.setUId(user.getId());
        List<Long> ids = shoppingVo.getIds();
        String pId = ids.stream()
                .map(Object::toString) // 将Long类型映射为String类型
                .collect(Collectors.joining(","));
        sysOrder.setPId(pId);
        List<Integer> count = shoppingVo.getCount();
        String counts = count.stream()
                .map(Object::toString) // 将Long类型映射为String类型
                .collect(Collectors.joining(","));
        sysOrder.setPCount(counts);
        orderMapper.insert(sysOrder);

    }
}
